package 力扣_链表.基础操作;

/**
 * @author zx
 * @create 2022-09-24 10:22
 */
public class 删除排序链表中的重复元素II_82 {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode dummy = new ListNode(101);
        dummy.next = head;
        ListNode prev = dummy;
        while(head != null){
            ListNode next = head.next;
            if(next == null){
                break;
            }
            //此时链表中至少有两个节点
            if(head.val == next.val){
                //head和next重复
                //让next一直向后移动,直到走到第一个与head.val不相等的节点
                while(next != null && next.val == head.val){
                    next = next.next;
                }
                //prev -> 第一个重复节点的前驱
                //next -> 最后一个重复节点的后继
                //prev -> next全是重复元素,需要删除
                prev.next = next;
                head = next;
            }else{
                //head与next不重复
                prev = prev.next;
                head = head.next;
            }
        }
        return dummy.next;
    }
}
